<?php
  //name: updateCountryDB
  //utility: use to update date in the 'country' table
  //input: $db, database login parameters
  //output: nothing
  
  function updateCountryDB($db)
  {
      //collect the number of lines in the 'globe' table
      $req = "SELECT COUNT(*) AS nbrLine FROM globe";
      $reply = $db->query($req);
      $datas = $reply->fetch();
      $nbrLine = $datas['nbrLine'];
     
	 
	 $INCREMENT = 1;
      for ($i = 1; $i <= $nbrLine; $i++) {
          //increase automatically at each loop
		        //request preparation
			
		  $req = $db->prepare('SELECT country FROM globe WHERE id=?');
		  $req->execute(array($i));
          $country_globe = $req->fetch();
          $country_tempo = $country_globe['country'];
          
          //collect the quantity of the line who match with the country
		  if($country_tempo != NULL)
		  {
          $req = $db->prepare('SELECT quantity FROM country WHERE name = ?');
          $req->execute(array($country_tempo));
          $quantity = $req->fetch();
          
          //if a line with country already exist, we increment the quantity value
          //else the line is created
          if ($quantity != null) {
              $req = $db->prepare('UPDATE country SET quantity = :quantity WHERE name = :name');
              $req->execute(array(
			  'quantity' => $quantity['quantity'] + $INCREMENT, 
			  'name' => $country_tempo
			  ));
          } else {
              $req = $db->prepare('INSERT INTO country(name, quantity) VALUES(:name, :quantity)');
              $req->execute(array('quantity' => $INCREMENT, 'name' => $country_tempo));
          }
		  }
      }
      
      
      echo "update of the globemag table finished" . '</br>';
  }
?>